package com.wwwarehouse.contract.contract_address;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.AddressListBean;
import com.wwwarehouse.contract.core.BaseViewPagerFragment;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.core.ContractEventCode;
import com.wwwarehouse.contract.core.EventManager;
import com.wwwarehouse.contract.event.ChooseAddressEvent;
import com.wwwarehouse.contract.event.ChooseAddressRefreshEvent;
import com.wwwarehouse.contract.event.ContractEvent;
import com.wwwarehouse.contract.event.RefreshConfirmOrderEvent;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/10/19.
 * 管理地址 viewpager
 */

public class ChooseAddressViewPagerFragment extends BaseViewPagerFragment {
	private boolean isShow;
	private String mAddressUkid;
	private AddressListBean addressListBean;

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		EventBus.getDefault().register(this);
		mCustomScroll.setOnPullListener(new ElasticScrollView.OnPullListener() {
			@Override
			public void onDownPull() {
				if (!isShow) {
					((BaseCardDeskActivity) mActivity).downPull_3();
					((BaseCardDeskActivity) mActivity).setTitleBt1Text("管理地址");
					((BaseCardDeskActivity) mActivity).showTitleBt1();
					((BaseCardDeskActivity) mActivity).setTitleBt1Enable(true);

				} else {
					((BaseCardDeskActivity) mActivity).hideTitleBt();
				}
				isShow = !isShow;

			}

			@Override
			public void onUpPull() {

			}
		});

	}

	@Override
	public void onResponse(String response, int what) {
		if (what == 1) {
			addressListBean = JSON.parseObject(response, AddressListBean.class);
			addressListBean.setmAddressUkid(mAddressUkid);
			if (addressListBean.getList() != null && addressListBean.getList().size() > 0) {
				setData(addressListBean, addressListBean.getList().size(), 7, ChooseAddressListViewFragment.class.getName());
			} else {
				mStateLayout.showEmptyView(true);
				mStateLayout.setEmptyListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						requestDatas();
					}
				});
			}
		}

	}

	@Override
	public void titleBt1Click() {
		if (peekFragment() instanceof ChooseAddressViewPagerFragment) {
			if (getArguments() != null && getArguments().getString(ContractConstant.KEY_CONSTRACT_PLACE_ORDER_DEMANID) != null) {
				Bundle bundle = new Bundle();
				String demanId = getArguments().getString(ContractConstant.KEY_CONSTRACT_PLACE_ORDER_DEMANID);
				bundle.putString(ContractConstant.KEY_CONSTRACT_PLACE_ORDER_DEMANID, demanId);
				bundle.putString(ContractConstant.KEY_ADDRESS_ADDRESSUKID, mAddressUkid);
				ManagerAddressViewPagerFragment managerAddressViewPagerFragment = new ManagerAddressViewPagerFragment();
				managerAddressViewPagerFragment.setArguments(bundle);
				pushFragment(managerAddressViewPagerFragment);
			}
		}
	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof ChooseAddressViewPagerFragment) {
			mActivity.setTitle("选择地址");
		}
	}

	@Override
	public void requestDatas() {
		if (getArguments() != null && getArguments().getString(ContractConstant.KEY_CONSTRACT_PLACE_ORDER_DEMANID) != null) {
			String demanId = getArguments().getString(ContractConstant.KEY_CONSTRACT_PLACE_ORDER_DEMANID);
			if (StringUtils.isNoneNullString(demanId)) {
				Map<String, Object> map = new HashMap<>();
				map.put("demandId", demanId);
				sendRequest(ContractConstant.ADDRESS_SELECT_ALL_ADDRESS_METHOD, map, 1);
			}

		}
		if (getArguments() != null && getArguments().getString(ContractConstant.KEY_ADDRESS_ADDRESSUKID) != null) {
			mAddressUkid = getArguments().getString(ContractConstant.KEY_ADDRESS_ADDRESSUKID);
		}
	}

	public void onEventMainThread(ChooseAddressRefreshEvent event) {
		if (peekFragment() instanceof ChooseAddressViewPagerFragment) {
			if (StringUtils.isNoneNullString(event.getRefresh())) {
				requestDatas();
			}
		}

	}

	public void onEventMainThread(BackListenerEvent backListenerEvent) {
		if (peekFragment() instanceof ChooseAddressViewPagerFragment && backListenerEvent.getMsg().equals("ChooseAddressViewPagerFragment")) {
			popFragment();
			//刷新选择地址界面
			if (addressListBean == null || addressListBean.getList() == null || addressListBean.getList().size() == 0) {
				RefreshConfirmOrderEvent refreshConfirmOrderEvent = new RefreshConfirmOrderEvent("refresh");
				EventManager.post(new ContractEvent<>(ContractEventCode.CODE_REFRESH_CONFIRM_ORDER, refreshConfirmOrderEvent));
				return;
			}
			boolean isClickOne = false;
			int index = 0;
			for (int i = 0; i < addressListBean.getList().size(); i++) {
				if (addressListBean.getList().get(i).isSelected()) {
					isClickOne = true;
					index = i;
				}
			}
			if (isClickOne) {
				EventBus.getDefault().post(new ChooseAddressEvent(addressListBean.getList().get(index)));
				return;
			}
			RefreshConfirmOrderEvent refreshConfirmOrderEvent = new RefreshConfirmOrderEvent("refresh");
			EventManager.post(new ContractEvent<>(ContractEventCode.CODE_REFRESH_CONFIRM_ORDER, refreshConfirmOrderEvent));
		}
	}


	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}
}
